package com.team6.stickynote.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.team6.stickynote.R;
import com.team6.stickynote.beans.StickyNoteBean;
import com.team6.stickynote.sql.DBAdapter;
import com.team6.stickynote.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddActivity extends AppCompatActivity {

    private EditText etTitle,etContent;

//    private NoteDbOpenHelper mNoteDbOpenHelper;
    private DBAdapter dbAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        dbAdapter = new DBAdapter(this);
        dbAdapter.open();
    }

    public void add(View view) {
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtil.toast("标题不能为空！");
            return;
        }

        StickyNoteBean note = new StickyNoteBean();
        note.setTitle(title);
        note.setText(content);
       // note.setCreatedTime(getCurrentTimeFormat());
        long row = dbAdapter.insert(note);
       // long row = mNoteDbOpenHelper.insertData(note);
        if (row != -1) {
            ToastUtil.toast("添加成功！");
            this.finish();
        }else {
            ToastUtil.toast("添加失败！");
        }

    }
    private String getCurrentTimeFormat() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    //SWC添加
//    public void add(View view) {
//        String title = etTitle.getText().toString();
//        String content = etContent.getText().toString();
//        if (TextUtils.isEmpty(title)) {
//            ToastUtil.toastShort(this, "标题不能为空！");
//            return;
//        }
//
//        Note note = new Note();
//
//        note.setTitle(title);
//        note.setContent(content);
//        note.setCreatedTime(getCurrentTimeFormat());
//        long row = mNoteDbOpenHelper.insertData(note);
//        if (row != -1) {
//            ToastUtil.toastShort(this,"添加成功！");
//            this.finish();
//        }else {
//            ToastUtil.toastShort(this,"添加失败！");
//        }
//
//    }


//    private AppBarConfiguration appBarConfiguration;
//    private ActivityAddBinding binding;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        binding = ActivityAddBinding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());
//
//        setSupportActionBar(binding.toolbar);
//
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_add);
//        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
//
//        binding.fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
//    }
//
//    @Override
//    public boolean onSupportNavigateUp() {
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_add);
//        return NavigationUI.navigateUp(navController, appBarConfiguration)
//                || super.onSupportNavigateUp();
//    }
//private String getCurrentTimeFormat() {
//    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd HH:mm:ss");
//    Date date = new Date();
//    return simpleDateFormat.format(date);
//}
}